<template>
  <div>
    <el-card style="margin: 20px 0px">
      <!-- 三级联动已经是全局组件了 -->
      <CategorySelect
        :show="scene !== 0"
        @getCategoryId="getCategoryId"
      ></CategorySelect>
    </el-card>
    <el-card>
      <!-- 底部这里将来是有三部分进行切换 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="spuList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <div>
                <!-- 这里按钮将来用hintButton替换 -->
                <HintButton
                  style="display: inline"
                  type="success"
                  icon="el-icon-plus"
                  size="mini"
                  title="添加sku"
                  @click="addSku(row)"
                ></HintButton>
                <HintButton
                  style="display: inline"
                  type="warning"
                  icon="el-icon-edit"
                  size="mini"
                  title="修改spu"
                  @click="updateSpu(row)"
                ></HintButton>
                <HintButton
                  style="display: inline"
                  type="info"
                  icon="el-icon-info"
                  size="mini"
                  title="查看当前spu全部sku列表"
                  @click="watchSku(row)"
                ></HintButton>

                <HintButton
                  slot="reference"
                  style="display: inline"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  @click="deleteSpu(row)"
                ></HintButton>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->, sizes,total"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        ref="spu"
        @changeScene="changeScene"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScene="changeScene"
      ></SkuForm>
    </el-card>

    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <!-- table展示sku列表-->
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
           <el-table-column prop="skuDesc" label="描述" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'
export default {
  name: 'Spu',
  components: {
    SpuForm,
    SkuForm
  },

  data() {
    return {
      // 分别是分类的id
      category1Id: '',
      category2Id: '',
      category3Id: '',
      // 控制三级联动的可操作性
      page: 1, // 分页器当前第几页
      limit: 3, // 每一页需要展示多少条数据
      spuList: [], // spu列表的数据
      total: 0, // 分页器一共需要展示数据的条数
      scene: 0, // 0代表展示SPU列表数据   1 添加SPU|修改SPU   2 添加SKU
      // 控制对话框的显示与隐藏
      dialogTableVisible: false,
      spu: {},
      skuList: [], // 存储的是SKU列表的数据
      loading: true
    }
  },

  mounted() {},

  methods: {
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level == 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        this.category3Id = categoryId
        this.getSpuList()
      }
    },
    async getSpuList(pager = 1) {
      this.page = pager
      // 获取分类的ID
      const { page, limit, category3Id } = this
      // 获取属性列表的数据
      const result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      if (result.code === 200) {
        this.total = result.data.total
        this.spuList = result.data.records
      }
    },
    handleSizeChange(limit) {
      // 修改参数
      this.limit = limit
      // 再发请求
      this.getSpuList()
    },
    changeScene({ scene, flag }) {
      this.scene = scene
      console.log(flag)
      if (flag) {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    addSpu() {
      // 切换为场景为1
      this.scene = 1
      this.$refs.spu.addSpuData(this.category3Id)
    },
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuData(row)
    },
    deleteSpu(row) {
      this.$confirm(`你确定删除${row.spuName}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 当用户点击确定按钮的时候会出发
          // 向服务器发请求
          const result = await this.$API.spu.reqDeleteSpu(row.id)
          // 如果删除成功
          if (result.code == 200) {
            this.$message.success('删除成功!')
            // 再次获取品牌列表数据
            this.getSpuList(this.spuList.length > 1 ? this.page : this.page - 1)
            //   }
          }
        })
        .catch(() => {
          // 当用户点击取消按钮的时候会触发
          this.$message.info('删除失败')
        })
    },
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    async watchSku(row) {
      this.dialogTableVisible = true
      this.spu = row
      let result = await this.$API.spu.reqSkuList(row.id)
      if (result.code == 200) {
        this.skuList = result.data
        //loading隐藏
        this.loading = false
      }
    }
  }
  // async deleteSpu(row) {
  //   const result = await this.$API.spu.reqDeleteSpu(row.id)
  //   if (result.code == 200) {
  //     this.$message({ type: 'success', message: '删除成功' })
  //     // 代表SPU个数大于1删除的时候停留在当前页，如果SPU个数小于1 回到上一页
  //     this.getSpuList(this.spuList.length > 1 ? this.page : this.page - 1)
  //   }
  // }
  // 添加SPU按钮的
}
</script>

<style scoped></style>
